package com.ruoyi.web.controller.mall.goodsGoods.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.controller.mall.goodsClass.service.IGoodsClassService;
import com.ruoyi.web.controller.mall.goodsGoods.domain.GoodsGoods;
import com.ruoyi.web.controller.mall.goodsGoods.service.IGoodsGoodsService;
import com.ruoyi.web.controller.mall.goodsImages.domain.GoodsImages;
import com.ruoyi.web.controller.mall.goodsImages.service.IGoodsImagesService;
import com.ruoyi.web.controller.mall.goodsProduct.domain.GoodsProduct;
import com.ruoyi.web.controller.mall.goodsProduct.service.IGoodsProductService;
import com.ruoyi.web.controller.mall.goodsType.service.IGoodsTypeService;
import com.ruoyi.web.controller.mall.goodsTypeParams.domain.GoodsTypeParams;
import com.ruoyi.web.controller.mall.goodsTypeParams.service.IGoodsTypeParamsService;
import com.ruoyi.web.controller.mall.goodsTypeSpec.domain.GoodsTypeSpec;
import com.ruoyi.web.controller.mall.goodsTypeSpec.service.IGoodsTypeSpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品Controller
 * 
 * @author ruoyi
 * @date 2021-01-18
 */
@RestController
@RequestMapping("/mall/goodsGoods")
public class GoodsGoodsController extends BaseController
{
    @Autowired
    private IGoodsGoodsService goodsGoodsService;
    @Autowired
    private IGoodsClassService goodsClassService;
    @Autowired
    private IGoodsImagesService goodsImagesService;
    @Autowired
    private IGoodsTypeParamsService goodsTypeParamsService;
    @Autowired
    private IGoodsTypeSpecService goodsTypeSpecService;
    @Autowired
    private IGoodsTypeService goodsTypeService;
    @Autowired
    private IGoodsProductService goodsProductService;

    /**
     * 查询商品列表
     */
//    @RequiresPermissions("mall:goodsGoods:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestBody GoodsGoods goodsGoods)
    {
        startPage();
        List<GoodsGoods> list = goodsGoodsService.selectGoodsGoodsList(goodsGoods);
        return getDataTable(list);
    }

    @PostMapping("/queryList")
    @ResponseBody
    public AjaxResult queryList(@RequestBody GoodsGoods goodsGoods)
    {
        List<Map<String,Object>> list = goodsGoodsService.queryList(goodsGoods);
        return AjaxResult.success(list);
    }

    /**
     * 获取商品规格名详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:notice:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        GoodsGoods goodsGoods = goodsGoodsService.selectGoodsGoodsById(id);
        //轮播图
        GoodsImages gi = new GoodsImages();
        gi.setGoodsId(id);
        gi.setAddress(0);
        List<GoodsImages> bannerList = goodsImagesService.selectGoodsImagesList(gi);
        goodsGoods.setBannerList(bannerList);
        //详情图
        gi.setAddress(1);
        List<GoodsImages> detailImgs = goodsImagesService.selectGoodsImagesList(gi);
        goodsGoods.setDetailImgList(detailImgs);

//        mmap.put("goodsClass", goodsClassService.queryAll();
        //获取商品类型对应的参数信息
//        List<GoodsTypeParams> typeParamsList = goodsTypeParamsService.queryByTypeId(goodsGoods.getTypeId());
        //获取商品类型对应的规格信息
//        List<GoodsTypeSpec> typeSpecList = goodsTypeSpecService.queryListAndValueByTypeId(goodsGoods.getTypeId());
//        goodsTypeService.queryAll();

        goodsGoods.setProductNum(goodsProductService.count(id));
        List<GoodsProduct> productList =goodsProductService.queryByGoodsId(id);
        if(productList.size()>0){
            for (GoodsProduct goodsProductDO : productList) {
                if(goodsProductDO.getIsDefault()==1){
                    goodsGoods.setGoodsProduct(goodsProductDO);
                }
            }
        }else{
            GoodsProduct goodsProductDO = new GoodsProduct();
            goodsProductDO.setPrice(new BigDecimal("0.00"));
            goodsGoods.setGoodsProduct(goodsProductDO);
        }
        goodsGoods.setProductList(productList);
        return AjaxResult.success(goodsGoods);
    }

    /**
     * 导出商品列表
     */
//    @RequiresPermissions("mall:goodsGoods:export")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(@RequestBody GoodsGoods goodsGoods)
    {
        List<GoodsGoods> list = goodsGoodsService.selectGoodsGoodsList(goodsGoods);
        ExcelUtil<GoodsGoods> util = new ExcelUtil<GoodsGoods>(GoodsGoods.class);
        return util.exportExcel(list, "goodsGoods");
    }

    /**
     * 新增保存商品
     */
//    @RequiresPermissions("mall:goodsGoods:add")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addSave(@RequestBody GoodsGoods goodsGoods)
    {
        return toAjax(goodsGoodsService.addGoodsGoods(goodsGoods));
    }

    /**
     * 修改保存商品
     */
//    @RequiresPermissions("mall:goodsGoods:edit")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult editSave(@RequestBody GoodsGoods goodsGoods)
    {
        return toAjax(goodsGoodsService.editGoodsGoods(goodsGoods));
    }

    /**
     * 修改保存商品
     */
//    @RequiresPermissions("mall:goodsGoods:edit")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PostMapping("/shelf")
    public AjaxResult shelf(@RequestBody GoodsGoods goodsGoods)
    {
        int dis = goodsGoods.getDisabled();
        GoodsGoods gg = new GoodsGoods();
        gg.setId(goodsGoods.getId());
        gg.setDisabled(dis);
        gg.setShopId(goodsGoods.getShopId());
        goodsGoodsService.updateGoodsGoods(gg);
        if (dis==0){
            return AjaxResult.success("上架成功");
        }else {
            return AjaxResult.success("下架成功");
        }
    }

    /**
     * 删除商品
     */
//    @RequiresPermissions("mall:goodsGoods:remove")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    public AjaxResult remove(@RequestBody GoodsGoods goodsGoods)
    {
        return toAjax(goodsGoodsService.deleteGoodsGoodsById(goodsGoods.getId()));
    }
}
